<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>群聊</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    .container{
      width: 410px;
      margin: 30px auto;
    }
    .user{
      margin: 8px 0;
      text-align: center;
    }
    #show{
      border: 1px solid #333;
      height: 300px;
      width: 400px;
      margin-bottom: 20px;
      overflow: auto;
    }
    .status{
      text-align: center;
      font-size: 12px;
      color: #999;
      margin: 4px;
    }
    .info{
      font-size: 12px;
      margin: 4px 6px;
    }
    .other{
      text-align: left;
    }
    .self{
      text-align: right;
    }
    .username{
      font-weight: bold;
      color: #999;
    }
    .content{
      font-size: 14px;
      color: #333;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="user">当前在前用户为：<span id="count"></span></div>
    <div id="show"></div>
    <input type="text" id="content" placeholder="内容">
    <input type="text" id="user" placeholder="用户名" value="" onchange="changeName(event)">
    <button id="send" onclick="send()">发送</button>
    <button id="exit" onclick="close()">退出</button>
  </div>
  <script>
    var user = 'user_' + getRowdomStr()
    getElm('#user').value = user
    // websocket
    var ws = new WebSocket('ws://localhost:8080')
    ws.onopen = function() {
      ws.send(JSON.stringify({
        code: 200,
        user: user
      }))
    }
    ws.onmessage = function(e) {
      console.log(e.data)
      var data = JSON.parse(e.data)
      var {user, code, content, currentUser} = data
      // 在线人数
      getElm('#count').innerText = data.count
      var showHtml = getElm('#show').innerHTML
      if (code === 200) {
        showHtml += `<div class="status">${user}进入聊天室</div>`
      } else if (code === 500) {
        showHtml += `<div class="status">${user}退出聊天室</div>`
      } else if (code === 304) {
        showHtml += `<div class="status">${user}更名为${currentUser}</div>`
      } else if (user !== getElm('#user').value){
        showHtml += `<div class="info other">
            <div class="username">${user}</div>
            <div class="msg">${content}</div>
          </div>`
      } else {
        showHtml += `<div class="info self">
            <div class="username">${user}</div>
            <div class="msg">${content}</div>
          </div>`
      }
      getElm('#show').innerHTML = showHtml
    }
    // 发送信息
    function send() {
      var content = getElm("#content").value
      var user = getElm("#user").value
      ws.send(JSON.stringify({
        user,
        content
      }))
    }

    function changeName(e) {
      var currentUser = e.target.value
      if (user !== currentUser) {
        ws.send(JSON.stringify({
          code: 304,
          user,
          currentUser
        }))
        user = currentUser
      }
    }

    // 退出群聊
    function close() {}

    function getRowdomStr(num=6) {
      var res = ''
      var str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
      for (var i=0; i<num; i++) {
        res += str.charAt(Math.floor(Math.random() * str.length))
      }
      return res
    }
    
    function getElm(selector, tag=document, isSingle=true) {
      return isSingle ? tag.querySelector(selector) : tag.querySelectorAll(selector)
    }
  </script>
</body>
</html>